
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Properties;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;


/**
 * 工具类。
 */
public class WebUtil {
	  /** 
     * 执行一个HTTP POST请求，返回请求响应的内容
     * @param url        请求的URL地址 
     * @param params 请求的查询参数,可以为null 
     * @return 返回请求响应的内容 
     */ 
     public static String doPost(String url, String body) { 
    	 StringBuffer stringBuffer = new StringBuffer();
			HttpEntity entity = null;
			BufferedReader in = null;
			HttpResponse response = null;
			try {
				DefaultHttpClient httpclient = new DefaultHttpClient();
				HttpParams params = httpclient.getParams();
				HttpConnectionParams.setConnectionTimeout(params, 20000);
				HttpConnectionParams.setSoTimeout(params, 20000);
				HttpPost httppost = new HttpPost(url);
				httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

				httppost.setEntity(new ByteArrayEntity(body.getBytes("UTF-8")));
				response = httpclient.execute(httppost);
				entity = response.getEntity();
				in = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
				String ln;
				while ((ln = in.readLine()) != null) {
					stringBuffer.append(ln);
					stringBuffer.append("\r\n");
				}
				httpclient.getConnectionManager().shutdown();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (IllegalStateException e2) {
				e2.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (null != in) {
					try {
						in.close();
						in = null;
					} catch (IOException e3) {
						e3.printStackTrace();
					}
				}
			}
			return stringBuffer.toString();
    } 

    /**
     * 读取配置信息。
     * @param key Key值
     * @return
     * @throws Exception
     */
    public static String getConfig(String key) throws Exception{
    	try{
	    	InputStream in = WebUtil.class.getResourceAsStream("/conf.Properties");
	        Properties p = new Properties();
	        p.load(in);
	        return p.get(key).toString().trim();
    	}catch(Exception e){
    		System.out.println("配置文件不存在"+e.toString());
    	}
    	return "";
    }
    
    /**
     * get 请求
     * @param domain
     * @return
     * @throws RequestErrorException
     */
    public static String getWebCon(String domain) {
		StringBuffer sb = new StringBuffer();
		try {
			java.net.URL url = new java.net.URL(domain);
			BufferedReader in = new BufferedReader(new InputStreamReader(url
					.openStream(),"UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			System.out.println(sb.toString());
			in.close();
		} catch (Exception e) { // Report any errors that arise
			sb.append(e.toString());
			System.err.println(e);
		}
		return sb.toString();
	}
    
    /**
     * 发送请求 post
     * @throws RequestErrorException 
     * */
    public static String getWebConPost(String urlStr, String params){ 
	    PrintWriter out = null; 
	    BufferedReader in = null; 
	    StringBuffer result = new StringBuffer(); 
	    try { 
		    URL realUrl = new URL(urlStr); 
		    // 打开和URL之间的连接 
		    URLConnection conn = realUrl.openConnection(); 
		    // 设置通用的请求属性 
		    conn.setRequestProperty("accept", "*/*"); 
		    conn.setRequestProperty("connection", "Keep-Alive"); 
		    conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 
		    // 发送POST请求必须设置如下两行 
		    conn.setDoOutput(true); 
		    conn.setDoInput(true); 
		    // 获取URLConnection对象对应的输出流 
		    out = new PrintWriter(conn.getOutputStream()); 
		    // 发送请求参数 
		    out.print(params); 
		    // flush输出流的缓冲 
		    out.flush(); 
		    // 定义BufferedReader输入流来读取URL的响应 
		    in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
		    String line = ""; 
		    while ((line = in.readLine()) != null) { 
		    result.append(line); 
		    } 
	    } catch (Exception e) { 
	    	result.append(e.toString());
			System.err.println(e);
	    } finally { 
		    try { 
			    if (out != null) { 
				    out.close(); 
				    out = null; 
			    } 
			    if (in != null) { 
				    in.close(); 
				    in = null; 
			    } 
		    } catch (Exception ex) { 
		    	
		    } 
	    } 
	    return result.toString(); 
    } 
    
    /** 
     * 执行一个HTTP POST请求，返回请求响应的内容
     * @param url        请求的URL地址 
     * @param params 请求的查询参数,可以为null 
     * @return 返回请求响应的内容 
     */ 
     public static String doPost(String url, String body) { 
    	 StringBuffer stringBuffer = new StringBuffer();
			HttpEntity entity = null;
			BufferedReader in = null;
			HttpResponse response = null;
			try {
				DefaultHttpClient httpclient = new DefaultHttpClient();
				HttpParams params = httpclient.getParams();
				HttpConnectionParams.setConnectionTimeout(params, 20000);
				HttpConnectionParams.setSoTimeout(params, 20000);
				HttpPost httppost = new HttpPost(url);
				httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

				httppost.setEntity(new ByteArrayEntity(body.getBytes("UTF-8")));
				response = httpclient.execute(httppost);
				entity = response.getEntity();
				in = new BufferedReader(new InputStreamReader(entity.getContent()));
				String ln;
				while ((ln = in.readLine()) != null) {
					stringBuffer.append(ln);
					stringBuffer.append("\r\n");
				}
				httpclient.getConnectionManager().shutdown();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (IllegalStateException e2) {
				e2.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (null != in) {
					try {
						in.close();
						in = null;
					} catch (IOException e3) {
						e3.printStackTrace();
					}
				}
			}
			return stringBuffer.toString();
    } 
     
     /** 
      * 执行一个HTTP POST请求，返回请求响应的内容
      * @param url        请求的URL地址 
      * @param params 请求的查询参数,可以为null 
      * @return 返回请求响应的内容 
      */ 
      public static String doPostFeiLiu(String url, String body) { 
     	 StringBuffer stringBuffer = new StringBuffer();
 			HttpEntity entity = null;
 			BufferedReader in = null;
 			HttpResponse response = null;
 			try {
 				DefaultHttpClient httpclient = new DefaultHttpClient();
 				HttpParams params = httpclient.getParams();
 				HttpConnectionParams.setConnectionTimeout(params, 20000);
 				HttpConnectionParams.setSoTimeout(params, 20000);
 				HttpPost httppost = new HttpPost(url);
 				httppost.setHeader("Content-Type", "text/html; charset=UTF-8");

 				httppost.setEntity(new ByteArrayEntity(body.getBytes("UTF-8")));
 				response = httpclient.execute(httppost);
 				entity = response.getEntity();
 				in = new BufferedReader(new InputStreamReader(entity.getContent()));
 				String ln;
 				while ((ln = in.readLine()) != null) {
 					stringBuffer.append(ln);
 					stringBuffer.append("\r\n");
 				}
 				httpclient.getConnectionManager().shutdown();
 			} catch (ClientProtocolException e) {
 				e.printStackTrace();
 			} catch (IOException e1) {
 				e1.printStackTrace();
 			} catch (IllegalStateException e2) {
 				e2.printStackTrace();
 			} catch (Exception e) {
 				e.printStackTrace();
 			} finally {
 				if (null != in) {
 					try {
 						in.close();
 						in = null;
 					} catch (IOException e3) {
 						e3.printStackTrace();
 					}
 				}
 			}
 			return stringBuffer.toString();
     } 
}
